package Operations;

import Book.Book;
import Book.BookList;

import java.util.Scanner;

public class DelOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        //删除图书
        System.out.println("删除图书！");
        System.out.println("请输入你要删除图书的名字：");
        Scanner sc = new Scanner(System.in);
        String name = sc.nextLine();

        //遍历寻找下标
        int index = -1;
        int size = bookList.getNumBook();
        for (int i = 0; i < size; i++) {
            Book book = bookList.getpos(i);
            if(name.equals(book.getBookname()))
            {
                index = i;
                break;
            }
        }

        //
        if(index == -1)
        {
            System.out.println("没有要删除的图书！");
            return ;
        }

        for (int i = index; i < size - 1; i++) {
            Book book = bookList.getpos(i);
            bookList.setBooks(book, i);
        }

        //每次删除后都置空
        bookList.setBooks(null,size - 1);
        bookList.setNumBook(size-1);
        System.out.println("删除成功");
    }

}
